package cn.web.tunnelrobot.controller;

import cn.web.tunnelrobot.service.WarningRecordService;
import cn.web.tunnelrobot.util.Constants;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/warningRecord")
@Api(tags = "警告记录接口")
@ApiSort(value = 6)
public class WarningRecordController {
    @Autowired
    private WarningRecordService warningRecordService;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
    //SimpleDateFormat simpleDateFormats = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");//获取当前时间
    //log4j
    Logger logger = LoggerFactory.getLogger(WarningRecordController.class);
    @GetMapping("/getWarningRecordList")
    @ResponseBody
    @ApiOperation(value = "按条件查询警告信息（包括环境警告记录页面（二期新增））========将wrType=1获取环境警告记录",notes = "按条件查询警告信息")
    public HashMap<String, Object> getWarningRecordList(@RequestParam(value ="wrLevel", required = false)String wrLevel,
                                                        @RequestParam(value ="wrType", required = false)Integer wrType,
                                                        @RequestParam(value ="wrDescribe", required = false)String wrDescribe,
                                                        @RequestParam(value ="wrAffirmStatus", required = false)Integer wrAffirmStatus,
                                                        @RequestParam(value ="wrWarningTime", required = false) String wrWarningTime,
                                                        @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                        @RequestParam(value = "pageSize", defaultValue = "20", required = false) Integer pageSize) throws ParseException {

       logger.info("警告列表传入的时间:"+wrWarningTime);
        Date wrWarningTimes = null;
        if(wrWarningTime != null && wrWarningTime.trim()!=""){
            wrWarningTimes = simpleDateFormat.parse(wrWarningTime);
        }
        return warningRecordService.getWarningRecordList(wrLevel,wrType,wrDescribe,wrAffirmStatus,wrWarningTimes,pageNo,pageSize);
    }
 /*   @ApiOperation(value = "添加预警信息", notes = "添加预警信息接口,前端勿掉" )
    @PostMapping("/addWarningRecordInfo")
    @ResponseBody
    public HashMap<String, Object> addWarningRecordInfo(@ApiParam("等级")@RequestParam(value ="wrLevel", required = false)String wrLevel,
                                                        @ApiParam("描述")@RequestParam(value ="wrDescribe", required = false)String wrDescribe){
        return warningRecordService.addWarningRecordInfo(wrLevel,wrDescribe);
    }*/
    @PostMapping("/batchConfirm")
    @ResponseBody
    public HashMap<String, Object> batchConfirm(@RequestParam(value ="wrAffirmUser") Integer wrAffirmUser,
                                                @RequestParam(value ="wrId")Integer wrId,
                                                @RequestParam(value ="wrAffirmStatus")Integer wrAffirmStatus){
       logger.info("wrAffirmUser:"+wrAffirmUser);
       logger.info("wrId:"+wrId);
       logger.info("wrAffirmStatus:"+wrAffirmStatus);
        return warningRecordService.batchConfirm(wrAffirmUser,wrId,wrAffirmStatus);
    }

    @GetMapping("/voiceBroadcast")
    @ResponseBody
    @ApiOperation(value = "警告等级，警告描述，警告时间",notes = "按条件查询警告信息")
    public  HashMap<String,Object> voiceBroadcast(@RequestParam("wrLevel")String wrLevel,
                                                  @RequestParam("wrDescribe")String wrDescribe,
                                                  @RequestParam("wrWarningTime") String wrWarningTime){
        HashMap<String,Object> map = new HashMap<>();
        if(wrLevel.equals("Ⅰ")){
            wrLevel = "一级";
        }else if(wrLevel.equals("Ⅱ")){
            wrLevel = "二级";
        }else if(wrLevel.equals("Ⅲ")){
            wrLevel = "三级";
        }else if(wrLevel.equals("Ⅳ")){
            wrLevel = "四级";
        }else if(wrLevel.equals("Ⅴ")){
            wrLevel = "五级";
        }
        try {
            String content = "警告发生于："+wrWarningTime+",晶高等级为:"+wrLevel+",警告描述:"+wrDescribe;
            String value = Constants.doPost(Constants.voiceBroadcast, "content=" + content);
            if(value.equals("success")){
                map.put("code",0);
                map.put("msg","语音播报成功!");
            }else{
                map.put("code",-1);
                map.put("msg","语音播报失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
